package com.atguigu.pratice;

public class TheInstanceList {
    public ListNode getIntersectionNode(ListNode headA,ListNode headB){
        int lenA=0;
        int lenB=0;
        ListNode nodeA=headA;
        ListNode nodeB=headB;
        //先进行二者长度的计算
        while(nodeA!=null){
            lenA++;
            nodeA=nodeA.next;
        }
        while(nodeB!=null){
            lenB++;
            nodeB=nodeB.next;
        }
        int len=lenA-lenB;
        ListNode fast=null;
        ListNode slow=null;
        if(len<0){
             fast=headB;
             slow=headA;
        }else{
            fast=headA;
            slow=headB;
        }
        int tmp=Math.abs(len);
        while(tmp!=0&&fast!=null){
            fast=fast.next;
            tmp--;
        }
        while(fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        if(fast==null)return null;
        return fast;
    }
}
